﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SoftwareEngineering_Project;

namespace SoftwareEngineering_Project
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        private SoftwareEngineering_Project.User CurrentUser
        {
            get
            {
                return (SoftwareEngineering_Project.User)Session["CurrentUser"];
            }
            set
            {
                Session["CurrentUser"] = value;
            }
        }

        private String IsRedirect
        {
            get
            {
                return (String)Session["IsRedirect"];
            }
            set
            {
                Session["IsRedirect"] = value;
            }
        }

        void Page_Load(Object sender, EventArgs e)
        {

            if (CurrentUser == null)
            {
                AnonymousPanel.Visible = true;
                LoggedInPanel.Visible = false;
                switch (Page.Title)
                {
                    case "Login":
                    case "About Us":
                    case "Confirm Registration":
                    case "Registration":
                        if (IsRedirect != null && IsRedirect == "T")
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "RedirectReason", "setTimeout (function () {" +
                                "alert('You must be Login to view the requested Page. Please Login or Register for an account');}, 500);", true);
                            IsRedirect = "F";
                        }
                        break;
                    default:
                        IsRedirect = "T";
                        Response.Redirect("~/Accounts/Login.aspx");
                        break;
                }
            }
            else
            {
                AnonymousPanel.Visible = false;
                LoggedInPanel.Visible = true;
                LoggedInLabel.Text = "Welcome " + CurrentUser.EmployeeData.FName + " " + CurrentUser.EmployeeData.LName;
                switch (Page.Title)
                {
                    case "Ambassador":
                        if (CurrentUser.RoleID != 11)
                        {
                            IsRedirect = "T";
                            Response.Redirect("~/Default.aspx");
                        }
                        break;
                    case "Home Page":
                        if (IsRedirect == "T")
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "RedirectReason2", "setTimeout (function () {" +
                                "alert('You must be an Ambassador to view the requested Page. Please login as an Ambassador');}, 500);", true);
                            IsRedirect = "F";
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        protected void Logout_LB_Click(Object sender, EventArgs e)
        {
            CurrentUser = null;
            Response.Redirect("~/Accounts/Login.aspx");
        }
    }
}
